1494A - ABC String - CodeForces Solution


bitmasks brute force implementation *900

Please click on ads to support us..

Python Code:

for s in[*open(0)][1:]:
 r=0,
 for u in s[:-1]:r+=r[-1]+2*(u==max('ABC',key=s.count))-1,
 print('YNEOS'[r[-1]|min(r)*max(r)!=0::2])

C++ Code:

using namespace std;
#include<bits/stdc++.h>
#include <iostream>
#include <cmath>
#define _DrIndex_ ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0);
#define clr(x,val) memset(x,val,sizeof(x))
#define read(FILE)  freopen(FILE, "r", stdin);
#define write(FILE) freopen(FILE, "w", stdout);
#define ll long long
#define  ul unsigned int
#define ull unsigned long long
#define ld long double

/*===================================================*/
bool preceed(pair<char,int> x , pair<char,int> y) {return x > y;}
/*===================================================*/
bool sortbyCond(const pair<int, int>& a,
                const pair<int, int>& b)
{
    if (a.first != b.first)
        return (a.first < b.first);
    else
        return (a.second > b.second);
}
/*===================================================*/
int factDigits(int n){
    if(n<0)return 0;if(n<=1)return 1;double digits=0;
    for (int i = 2; i <= n; ++i) {
        digits+= log10(i);
    }
    return floor(digits)+1;
}
/*===================================================*/
void primeFactors(int n){int c=2;while(n>1){if(n%c==0){std::cout<<c<<" ";n/=c;}else c++;}}
/*===================================================*/
bool cmp(std::pair<std::string,int>&a,std::pair<std::string,int>&b){return a.second>b.second;}
/*===================================================*/
bool absPreceed(int x , int y){return abs(x)< abs(y);}
/*===================================================*/
ll noOfDigits(ll n){return 1+floor(log10(n));}
/*===================================================*/
ll gcd(ll a, ll b)
{
    if (a == 0) return b;
    return gcd(b%a, a);
}
/*===================================================*/
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b))*b;
}
/*===================================================*/
// kadane's algorithm for getting max sum of sub array in O(n)
int maxSubArraySum(int array[], int size)
{
    int max_so_far = INT_MIN, max_ending_here = 0;

    for (int i = 0; i < size; i++)
    {
        max_ending_here = max_ending_here + array[i];
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;

        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}

/*===================================================*/
long long MOD = 1e9 + 7;

ll fast_power(ll base , ll power){
    ll  res=1;
    while (power > 0){
        if(power & 1){
            res=(res*base)%MOD;
        }
        base = (base*base) % MOD;
        power >>= 1;
    }
    return res;
}
/*===================================================*/
// get no. ones in the binary representation
int popCount(int num){
    int res=0;
    while(num > 0){
        res += num & 1;
        num >>= 1;
    }return res;
}
/*===================================================*/

void read_Write() {
    ios::sync_with_stdio(false),
            cin.tie(nullptr),
            cout.tie(nullptr);
#ifndef ONLINE_JUDGE
    read("input.txt")
    write("output.txt")
#else

#  endif
}

const int N = 1e6+5;
int main() {
    _DrIndex_
    //read_Write();
    int tt=1;cin>>tt;
    while (tt--) {
        string s;cin >> s;char a[3]{'A' , 'B' , 'C'};bool ff=false;
        for(int mask = 1 ; mask < (1 << 3) ; mask++){
            string res;
            for(int i = 0 ; i < s.size() ; i++){
                int ind = distance(a,find(a,a+3,s[i]));
                if(mask & (1 << ind))res+='(';
                else res+=')';
            }
            stack<char> st;bool f=false;
            for (int i = 0; i < res.size(); i++) {
                if (res[i] == '(') st.push(s[i]);
                else {
                    if (st.empty()) {f =true;break;}
                    else st.pop();
                }
            }if(st.empty() && f == false){ff=true;break;}
        }cout << (ff ? "YES" : "NO") << '\n';
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1538A - Stone Game
1454C - Sequence Transformation
165B - Burning Midnight Oil
17A - Noldbach problem
1350A - Orac and Factors
1373A - Donut Shops
26A - Almost Prime
1656E - Equal Tree Sums
1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains
664A - Complicated GCD
1635D - Infinite Set
1462A - Favorite Sequence
1445B - Elimination
1656C - Make Equal With Mod
567A - Lineland Mail
1553A - Digits Sum
1359B - New Theatre Square
766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums